﻿using System;

namespace JESAI.Core.Util
{

    /// <summary>
    /// 释放操作
    /// </summary>
    public partial class DisposeAction : IDisposable
    {
        /// <summary>
        /// 操作
        /// </summary>
        private readonly Action _action;

        /// <summary>
        /// 初始化一个<see cref="DisposeAction"/>类型的实例
        /// </summary>
        /// <param name="action">操作</param>
        public DisposeAction(Action action) => _action = action ?? throw new ArgumentNullException(nameof(action));

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose() => _action();
    }

    public partial class DisposeAction<T> : IDisposable
    {
        private readonly Action<T> _action;

        private readonly T _parameter;

        /// <summary>
        /// Creates a new <see cref="DisposeAction"/> object.
        /// </summary>
        /// <param name="action">Action to be executed when this object is disposed.</param>
        /// <param name="parameter">The parameter of the action.</param>
        public DisposeAction(Action<T> action, T parameter)
        {
            _action = action;
            _parameter = parameter;
        }

        public void Dispose()
        {
            _action(_parameter);
        }
    }
}